package double_slider;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.LabelBuilder;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class DoubleSliderDemo extends Application {
	
	private static final NumberFormat twoDp = new DecimalFormat("0.##");
	private static final Color foregroundColor = Color.WHITE;

	@Override
	public void start(Stage stage) throws Exception {
		Group root = new Group();
		Scene scene = new Scene(root, 500, 300, Color.BLACK);
		stage.setScene(scene);
		scene.getStylesheets().addAll(DoubleSliderDemo.class.getResource("double_slider.css").toExternalForm());
		VBox vbox = new VBox(15);
		final Label title = new Label("DoubleSlider Demo");
		title.setStyle("-fx-font-size: 20px; -fx-text-fill: white");
		final DoubleSlider doubleSlider1 = new DoubleSlider();
		doubleSlider1.setShowTickMarks(true);
		doubleSlider1.setShowTickLabels(true);
		doubleSlider1.setMajorTickUnit(10f);

		final DoubleSlider doubleSlider2 = new DoubleSlider();
		doubleSlider2.setMin(150);
		doubleSlider2.setMax(500);
		doubleSlider2.setShowTickMarks(true);
		doubleSlider2.setShowTickLabels(true);
		doubleSlider2.setMajorTickUnit(50f);
		
		vbox.setAlignment(Pos.CENTER);
		vbox.setPrefWidth(500);
		vbox.getChildren().addAll(title, createSliderBox(doubleSlider1, false), createSliderBox(doubleSlider2, true));

		root.getChildren().add(vbox);
		stage.show();
	}
	
	public static HBox createSliderBox(DoubleSlider dSlider, boolean vertical) {
		
		if (vertical)
			dSlider.orientationProperty().setValue(Orientation.VERTICAL);
		Label label_lower = LabelBuilder.create()
    			.text("Lower: ")
    			.textFill(foregroundColor).build();
		Label label_upper = LabelBuilder.create()
    			.text("Upper: ")
    			.textFill(foregroundColor).build();
		final Label valuelabel_lower = LabelBuilder.create()
    			.text(twoDp.format(dSlider.getValue1()))
    			.textFill(foregroundColor).build();
		final Label valuelabel_upper = LabelBuilder.create()
    			.text(twoDp.format(dSlider.getValue2()))
    			.textFill(foregroundColor).build();
		dSlider.value1Property().addListener(new ChangeListener<Number>() {

			@Override
			public void changed(ObservableValue<? extends Number> arg0,
					Number arg1, Number arg2) {
				valuelabel_lower.setText(twoDp.format(arg2.doubleValue()));
				
			}
		});
		dSlider.value2Property().addListener(new ChangeListener<Number>() {

			@Override
			public void changed(ObservableValue<? extends Number> arg0,
					Number arg1, Number arg2) {
				valuelabel_upper.setText(twoDp.format(arg2.doubleValue()));
				
			}
		});
		HBox labelBox_lower = new HBox(5);
		labelBox_lower.getChildren().addAll(label_lower, valuelabel_lower);
		HBox labelBox_upper = new HBox(5);
		labelBox_upper.getChildren().addAll(label_upper, valuelabel_upper);
		
		VBox labelBox = new VBox(5);
		labelBox.setPrefWidth(100);
		labelBox.setAlignment(Pos.CENTER);
		labelBox.getChildren().addAll(labelBox_lower, labelBox_upper);
		
		HBox sliderBox = new HBox(30);
		sliderBox.getChildren().addAll(dSlider, labelBox);
		sliderBox.setAlignment(Pos.CENTER);
		
		return sliderBox;
		
	}

	public static void main(String[] args) {
		launch(args);
	}



}
